#!/bin/bash

# 打包脚本 for zichan project
# 使用方法: ./package.sh [clean|deb|rpm|all]

set -e  # 遇到错误立即退出

PROJECT_NAME="zichan"
BUILD_DIR="build"
PACKAGE_DIR="package"
VERSION=$(grep "project.*VERSION" CMakeLists.txt | awk '{print $3}' | tr -d ')')

# 显示帮助信息
show_help() {
    echo "Usage: $0 [clean|deb|rpm|tgz|all|help]"
    echo "  clean    - 清理构建文件"
    echo "  deb      - 构建并生成 DEB 包"
    echo "  rpm      - 构建并生成 RPM 包"
    echo "  tgz      - 构建并生成 tar.gz 包"
    echo "  all      - 构建并生成所有格式的包"
    echo "  help     - 显示帮助信息"
    echo ""
    echo "如果没有参数，默认生成 DEB 包"
}

# 清理函数
clean_build() {
    echo "🧹 清理构建文件..."
    rm -rf ${BUILD_DIR} ${PACKAGE_DIR} *.deb *.rpm *.tar.gz *.zip
    echo "✅ 清理完成"
}

# 检查依赖
check_dependencies() {
    local missing_deps=()
    
    # 检查必要的工具
    for cmd in cmake make g++; do
        if ! command -v $cmd &> /dev/null; then
            missing_deps+=("$cmd")
        fi
    done

    # 检查打包工具
    if [[ "$1" == "deb" || "$1" == "all" ]]; then
        if ! command -v dpkg &> /dev/null; then
            echo "⚠️  注意: dpkg 未找到，DEB 包可能无法生成"
        fi
    fi

    if [[ "$1" == "rpm" || "$1" == "all" ]]; then
        if ! command -v rpmbuild &> /dev/null; then
            echo "⚠️  注意: rpmbuild 未找到，RPM 包可能无法生成"
        fi
    fi

    if [ ${#missing_deps[@]} -ne 0 ]; then
        echo "❌ 缺少必要的依赖: ${missing_deps[*]}"
        exit 1
    fi
}

# 配置和构建项目
build_project() {
    echo "🔨 开始构建项目..."
    
    # 创建构建目录
    mkdir -p ${BUILD_DIR}
    cd ${BUILD_DIR}
    
    # 配置项目
    echo "📋 运行 CMake..."
    cmake .. -DCMAKE_BUILD_TYPE=Release
    
    # 编译项目
    echo "⚙️  编译项目..."
    make -j$(nproc)
    
    cd ..
    echo "✅ 构建完成"
}

# 生成 DEB 包
package_deb() {
    echo "📦 生成 DEB 包..."
    cd ${BUILD_DIR}
    cpack -G DEB
    mv *.deb ..
    cd ..
    echo "✅ DEB 包生成完成: $(ls *.deb)"
}

# 生成 RPM 包
package_rpm() {
    echo "📦 生成 RPM 包..."
    cd ${BUILD_DIR}
    cpack -G RPM
    mv *.rpm ..
    cd ..
    echo "✅ RPM 包生成完成: $(ls *.rpm)"
}

# 生成 TGZ 包
package_tgz() {
    echo "📦 生成 tar.gz 包..."
    cd ${BUILD_DIR}
    cpack -G TGZ
    mv *.tar.gz ..
    cd ..
    echo "✅ tar.gz 包生成完成: $(ls *.tar.gz)"
}

# 生成所有格式的包
package_all() {
    echo "📦 生成所有格式的包..."
    cd ${BUILD_DIR}
    cpack -G "DEB;RPM;TGZ"
    mv *.deb *.rpm *.tar.gz ..
    cd ..
    echo "✅ 所有包生成完成"
    ls -la *.deb *.rpm *.tar.gz
}

# 主函数
main() {
    local action=${1:-"deb"}  # 默认生成 DEB 包
    
    case "${action}" in
        "clean")
            clean_build
            ;;
        "deb")
            check_dependencies "deb"
            build_project
            package_deb
            ;;
        "rpm")
            check_dependencies "rpm"
            build_project
            package_rpm
            ;;
        "tgz")
            build_project
            package_tgz
            ;;
        "all")
            check_dependencies "all"
            build_project
            package_all
            ;;
        "help"|"-h"|"--help")
            show_help
            ;;
        *)
            echo "❌ 未知操作: ${action}"
            show_help
            exit 1
            ;;
    esac
}

# 运行主函数
main "$@"